<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@include file="../head.jsp"%>
<html>
<head>
<script type="text/javascript">
(function(){
    Ext.onReady(function(){
        Ext.QuickTips.init();
        var id = '';

        Ext.define('Group', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'id',type:'string'},
                {name:'name',type:'string'},
                {name:'groupDesc',type:'string'},
                {name:'status',type:'int'},
                {name:'version',type:'int'}
            ]
        });

        Ext.define('SysOperator', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'id',type:'string'},
                {name:'name',type:'string'},
                {name:'sex',type:'string'},
                {name:'phone',type:'string'},
                {name:'qq',type:'string'},
                {name:'mail',type:'string'},
                {name:'departmentId',type:'string'},
                {name:'departmentName',type:'string'},
                {name:'idCard',type:'string'},
                {name:'version',type:'int'},
                {name:'status',type:'int'}
            ]
        });

        var sm = Ext.create('Ext.selection.CheckboxModel');

        var queryName = Ext.create('Ext.form.field.Text',{
            labelWidth:55,
            id:'queryName',
            name:'queryName'
        });

        var department = Ext.create('Ext.form.ComboBox',{
            queryMode: 'local',
            displayField: 'label',
            valueField: 'value',
            store:Ext.create('Ext.data.Store', {
                model: 'commonModel',
                proxy: {
                    type:'ajax',
                    url:'/fsxt/sysBase!getDepartment.action'
                },
                autoLoad: true,
                listeners:{
                    load:function(){
                        var insert = Ext.create('commonModel',{value:'',label:'0-全部'});
                        this.insert(0,insert);
                    }
                }
            })
        });


        var operatorGrid = Ext.create('Ext.grid.Panel', {
            title: '人员列表',
            autoScroll:true,
            loadMask: true,
            columns:[
                {text: "人员名称",dataIndex:'name',sortable: true},
                {text: "电话",dataIndex:'phone',sortable: true},
                {text: "所属部门",dataIndex:'departmentName',sortable: true}
            ],
            tbar:Ext.create('Ext.toolbar.Toolbar',{
                items:['-',{
                    xtype:'label',
                    text:'人员名称:'
                },queryName,'-',{
                    xtype:'label',
                    text:'所属部门:'
                },department,'-',{
                    xtype:'button',
                    iconCls:'find',
                    text:'查询',
                    handler:function(){
                        operatorGrid.store.load();
                    }
                },'-']
            }),
            store:Ext.create('Ext.data.Store',{
                model: 'SysOperator',
                proxy: {
                    type:'ajax',
                    url:'/fsxt/sys/sysOperator!getSysOperator.action',
                    reader: {
                        root: 'root',
                        totalProperty: 'totalProperty'
                    }
                },
                autoLoad: true,
                listeners:{
                    beforeload:function(){
                        this.proxy.extraParams = {
                            name:encodeURIComponent(queryName.getValue()),
                            departmentId:department.getValue(),
                            status:1
                        }
                    }
                }
            }),
            listeners:{
                itemclick:function(view,record,item,index,event,options){
                    id = record.data.id;
                    requestGroup(record.data.id);
                }
            }
        });

        var groupGrid = Ext.create('Ext.grid.Panel',{
            title: '用户组列表',
            selModel:sm,
            loadMask: true,
            autoScroll:true,
            columns:[
                Ext.create('Ext.grid.RowNumberer'),
                {text: "用户组名",dataIndex: 'name',sortable: true},
                {text: "用户组描述",width:250,dataIndex: 'groupDesc',sortable: true}
            ],
            tbar:Ext.create('Ext.toolbar.Toolbar',{
                items:['-',{
                    xtype:'button',
                    iconCls:'find',
                    text:'保存',
                    handler:function(){
                        if(SysBase.isBlank(id)){
                            Ext.Msg.show({title:'提示',msg:'未能获取用户',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        var ids = '';
                        var selected = groupGrid.getView().getSelectionModel().getSelection();
                        for(var i = 0 ; i < selected.length ; i ++){
                            var record = selected[i];
                            ids = ids + record.data.id + ',';
                        }
                        var params = {};
                        params.id = id;
                        params.ids = ids;
                        var data = {};
                        data.params = params;
                        data.url = '/fsxt/sys/groupOperator!saveOperatorGroup.action';
                        callAjax.call(data);
                    }
                },'-']
            }),
            store:Ext.create('Ext.data.Store',{
                model: 'Group',
                proxy: {
                    type:'ajax',
                    url:'/fsxt/sys/group!getGroup.action',
                    reader: {
                        root: 'root',
                        totalProperty: 'totalProperty'
                    }
                },
                autoLoad: true,
                listeners:{
                    beforeload:function(){
                        groupGrid.store.proxy.extraParams = {
                            status:1
                        }
                    }
                }
            })
        });

        function requestGroup(id){
            Ext.MessageBox.wait("", "正在请求,请稍候...");
            Ext.Ajax.request({
                url:'/fsxt/sys/groupOperator!getGroup.action',
                method: 'post',
                params: {id:id},
                success:function(response,options){
                    Ext.MessageBox.hide();
                    var ids = response.responseText.split(',');
                    var records = [];
                    groupGrid.store.each(function(record){
                        for(var i = 0 ; i < ids.length ; i ++){
                            if(record.data.id == ids[i]){
                                records.push(record);
                            }
                        }
                    });
                    groupGrid.getSelectionModel().select(records);
                },
                failure:function(response,options){
                    Ext.MessageBox.hide();
                    Ext.Msg.show({title:'提示',msg:'获取此角色对应用户失败!.',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.ERROR});
                }
            });
        }

        new Ext.Viewport({
            border:false,
            layout:'border',
            items:[{
                width:550,
                region:'west',
                split:true,
                items: [operatorGrid]
            },{
                region:'center',
                items: [groupGrid]
            }]
        });

    });
})();
</script>
</head>
<body>
</body>
</html>